package com.jsh.erp.service.auditSynchronization;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.AuditSynchronization;
import com.jsh.erp.datasource.entities.AuditSynchronizationExample;
import com.jsh.erp.datasource.mappers.AccountHeadMapperEx;
import com.jsh.erp.datasource.mappers.AuditSynchronizationMapper;
import com.jsh.erp.datasource.mappers.DepotHeadMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuditSynchronizationService {
    private Logger logger = LoggerFactory.getLogger(AuditSynchronizationService.class);

    @Resource
    private AuditSynchronizationMapper auditSynchronizationMapper;
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;
    @Resource
    private AccountHeadMapperEx accountHeadMapperEx;
    @Resource
    private DepotHeadMapperEx depotHeadMapperEx;
    @Resource
    private SystemConfigService systemConfigService;

    public AuditSynchronization getAuditSynchronization(String id)throws Exception {
        AuditSynchronization result=null;
        try{
            result=auditSynchronizationMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public List<AuditSynchronization> getAuditSynchronizationListByIds(String ids)throws Exception {
        List<String> idList = StringUtil.strToStringList(ids);
        List<AuditSynchronization> list = new ArrayList<>();
        try{
            AuditSynchronizationExample example = new AuditSynchronizationExample();
            example.createCriteria().andIdIn(idList);
            list = auditSynchronizationMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<AuditSynchronization> getAuditSynchronization()throws Exception {
        AuditSynchronizationExample example = new AuditSynchronizationExample();
        example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<AuditSynchronization> list=null;
        try{
            list=auditSynchronizationMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public List<AuditSynchronization> select(String name, int offset, int rows)throws Exception {
        List<AuditSynchronization> list=null;
        try{
            AuditSynchronizationExample example=new AuditSynchronizationExample();
            example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
            list=auditSynchronizationMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list;
    }

    public Long countAuditSynchronization(String name, String type)throws Exception {
        Long result=null;
        try{
            AuditSynchronizationExample example=new AuditSynchronizationExample();
            example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
            result=auditSynchronizationMapper.countByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertAuditSynchronization(JSONObject obj, HttpServletRequest request)throws Exception {
        AuditSynchronization auditSynchronization = JSONObject.parseObject(obj.toJSONString(), AuditSynchronization.class);
        auditSynchronization.setUploadFlag("0");
        int result=0;
        try{
            result=auditSynchronizationMapper.insertSelective(auditSynchronization);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateAuditSynchronization(JSONObject obj, HttpServletRequest request)throws Exception {
        AuditSynchronization auditSynchronization = JSONObject.parseObject(obj.toJSONString(), AuditSynchronization.class);
        auditSynchronization.setUploadFlag("0");
        int result=0;
        try{
            result=auditSynchronizationMapper.updateByPrimaryKeySelective(auditSynchronization);
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateByIdList(String auditList)throws Exception {
        List<String> ids = StringUtil.strToStringList(auditList);
        AuditSynchronization auditSynchronization = new AuditSynchronization();
        auditSynchronization.setUploadFlag("1");
        AuditSynchronizationExample example=new AuditSynchronizationExample();
        example.createCriteria().andIdIn(ids);
        return auditSynchronizationMapper.updateByExampleSelective(auditSynchronization,example);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteAuditSynchronization(String id, HttpServletRequest request)throws Exception {
        return batchDeleteAuditSynchronizationByIds(id.toString());
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteAuditSynchronization(String ids, HttpServletRequest request) throws Exception{
        return batchDeleteAuditSynchronizationByIds(ids);
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteAuditSynchronizationByIds(String ids)throws Exception {
        int result =0;
        String [] idArray=ids.split(",");
        try{
            for (String id : idArray){
                result=auditSynchronizationMapper.deleteByPrimaryKey(id);
            }
        }catch(Exception e){
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    public int checkIsNameExist(String id, String name) throws Exception{
        AuditSynchronizationExample example = new AuditSynchronizationExample();
        example.createCriteria().andIdNotEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<AuditSynchronization> list =null;
        try{
            list=auditSynchronizationMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list==null?0:list.size();
    }
}
